<template>
  <div class="detail-container">
    <!-- 头部信息 -->
    <el-card class="header-card" shadow="never">
      <div class="header">
        <div class="info">
          <h1 class="title">{{ model.title }}</h1>
          <div class="company-info">
            <el-icon class="company-icon"><OfficeBuilding /></el-icon>
            <span class="company-name">{{ model.company }}</span>
          </div>
          <div class="meta">
            <el-tag type="primary" class="meta-tag">{{ model.jobType }}</el-tag>
            <el-tag type="success" class="meta-tag">
              <span v-if="model.minSalary > 0">{{ formatSalary(model.minSalary) }} - </span>
              <span v-if="model.maxSalary > 0">{{ formatSalary(model.maxSalary) }}</span>
              <div v-else>面议</div>
            </el-tag>
            <el-tag type="info" class="meta-tag">
              <el-icon><Location /></el-icon>
              {{ model.location }}
            </el-tag>
          </div>
        </div>
        <div class="company-avatar" v-if="false">
          <el-avatar :size="80" :src="model.face" />
        </div>
      </div>
    </el-card>

    <!-- 职位详情 -->
    <el-row :gutter="20" class="content-row">
      <el-col :xs="24" :lg="16">
        <!-- 职位描述 -->
        <el-card class="detail-card" shadow="never">
          <template #header>
            <div class="card-header">
              <el-icon><Document /></el-icon>
              <span>职位描述</span>
            </div>
          </template>
          <div class="description">
            <div class="section" v-if="model.responsibilities">
              <h3>工作职责</h3>
              <p>{{ model.responsibilities }}</p>
            </div>
            <div class="section">
              <h3>任职要求</h3>
              <p>{{ model.requirements }}</p>
            </div>
          </div>
        </el-card>

        <!-- 职位要求 -->
        <el-card class="detail-card" shadow="never">
          <template #header>
            <div class="card-header">
              <el-icon><User /></el-icon>
              <span>任职要求</span>
            </div>
          </template>
          <el-row :gutter="16" class="requirements-grid">
            <el-col :span="12">
              <div class="requirement-item">
                <span class="label">学历要求：</span>
                <span class="value">{{ model.education }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="requirement-item">
                <span class="label">工作经验：</span>
                <span class="value">{{ model.experience }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="requirement-item">
                <span class="label">工作类型：</span>
                <span class="value">{{ model.jobType }}</span>
              </div>
            </el-col>
            <el-col :span="12">
              <div class="requirement-item">
                <span class="label">薪资待遇：</span>
                <span class="value">
                  <span v-if="model.minSalary > 0">{{ formatSalary(model.minSalary) }} - </span>
                  <span v-if="model.maxSalary > 0">{{ formatSalary(model.maxSalary) }}</span>
                  <div v-else>面议</div>
                </span>
              </div>
            </el-col>
          </el-row>
        </el-card>
      </el-col>

      <el-col :xs="24" :lg="8">
        <!-- 联系方式 -->
        <el-card class="contact-card" shadow="never">
          <template #header>
            <div class="card-header">
              <el-icon><Phone /></el-icon>
              <span>联系方式</span>
            </div>
          </template>
          <div class="contact-info">
            <div class="contact-item" v-for="(item, index) in phoneNumbers">
              <el-icon class="contact-icon" v-if="item.phone"><Phone /></el-icon>
              <div class="contact-details" v-if="item.phone">
                <span class="contact-label" v-if="item.name">{{ item.name }}</span>
                <span class="contact-label" v-else>联系电话</span>
                <span class="contact-value">{{ item.phone }}</span>
              </div>
            </div>
            <div class="contact-item">
              <el-icon class="contact-icon"><MapLocation /></el-icon>
              <div class="contact-details">
                <span class="contact-label">工作地点</span>
                <span class="contact-value">{{ model.location }}</span>
              </div>
            </div>
            <div class="contact-item">
              <el-icon class="contact-icon"><Calendar /></el-icon>
              <div class="contact-details">
                <span class="contact-label">发布时间</span>
                <span class="contact-value">{{ formatDate(model.postedDate) }}</span>
              </div>
            </div>
          </div>

          <div class="action-buttons">
            <el-button type="primary" size="large" @click="handleCall" class="action-btn">
              <el-icon><Phone /></el-icon>
              立即联系
            </el-button>
            <el-button size="large" @click="handleFavorite" class="action-btn" :icon="Star">
              {{ isFavorited ? '已收藏' : '收藏职位' }}
            </el-button>
          </div>
        </el-card>

        <!-- 公司信息 -->
        <el-card class="company-card" shadow="never" v-if="false">
          <template #header>
            <div class="card-header">
              <el-icon><OfficeBuilding /></el-icon>
              <span>公司信息</span>
            </div>
          </template>
          <div class="company-details">
            <div class="company-logo">
              <el-avatar :size="60" :src="model.face" />
            </div>
            <div class="company-info-detail">
              <h4>{{ model.company }}</h4>
              <p class="company-desc">专业的生态渔庄，致力于为客户提供优质的餐饮服务和生态体验。</p>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { OfficeBuilding, Location, Document, User, Phone, MapLocation, Calendar, Star } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

import http from '@/apis/zhaopin'
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const route = useRoute()

// 招聘数据
const model = ref({
  title: '招聘服务员',
  company: '虹鳟鱼生态渔庄',
  jobType: '全职',
  tagsJson: '[]',
  location: '东川区态渔庄',
  salaryRange: '面议',
  responsibilities: '招聘服务员2名',
  requirements: '要求女性、年龄25-40岁',
  postedDate: '2025-09-01 21:19:18',
  status: 100,
  face: '',
  minSalary: 0,
  maxSalary: 10,
  contactPerson: '',
  phone: '109902',
  education: '不限',
  experience: '不限',
  addressName: null,
  position: null,
  id: '7159253'
})

const phoneNumbers = ref<Array<any>>([])

const isFavorited = ref(false)
// 格式化换行符号
const formatTextHtml = (str: string) => {
  if (!str) {
    return ''
  }
  let html = str.replace('\r', '').replace('\n', '<br/>')
  console.log(str)
  return str
}

const formatSalary = (num: number) => {
  // 处理数值为0的情况
  if (num === 0) return '0'

  // 大于等于1000时，转换为k单位
  if (num >= 1000) {
    // 除以1000，保留1位小数（如2500 → 2.5）
    const value = (num / 1000).toFixed(1)
    // 去除末尾的.0（如3000 → 3k 而非3.0k）
    return value.endsWith('.0') ? `${parseInt(value, 10)}k` : `${value}k`
  }
  // 小于1000时直接返回原数值
  return num.toString()
}

// 格式化日期
const formatDate = (dateStr: string) => {
  const date = new Date(dateStr)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  })
}

// 拨打电话
const handleCall = () => {
  window.location.href = `tel:${model.value.phone}`
}

// 收藏职位
const handleFavorite = () => {
  isFavorited.value = !isFavorited.value
  ElMessage.success(isFavorited.value ? '收藏成功' : '取消收藏')
}

onMounted(() => {
  if (route.query.id) {
    http.fetchById(route.query.id as string).then((response) => {
      let { code, message, result } = response.data
      if (code !== 200) {
        ElMessage.error(message)
        return
      }
      // console.log(result)
      model.value = result
      if (result.phoneNumbers) {
        phoneNumbers.value = JSON.parse(result.phoneNumbers)
        // console.log(phoneNumbers.value)
      }
    })
  }
})
</script>

<style scoped>
.detail-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.header-card {
  margin-bottom: 20px;
  border-radius: 12px;
  border: none;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 20px;
}

.info {
  flex: 1;
}

.title {
  font-size: 28px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 12px 0;
}

.company-info {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}

.company-icon {
  color: #409eff;
}

.company-name {
  font-size: 16px;
  color: #606266;
  font-weight: 500;
}

.meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.meta-tag {
  display: flex;
  align-items: center;
  gap: 4px;
}

.company-avatar {
  flex-shrink: 0;
}

.content-row {
  margin-bottom: 20px;
}

.detail-card,
.contact-card,
.company-card {
  margin-bottom: 20px;
  border-radius: 12px;
  border: none;
}

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: #2c3e50;
}

.description .section {
  margin-bottom: 24px;
  white-space: pre-line;
}

.description h3 {
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 12px 0;
  padding-bottom: 8px;
  border-bottom: 2px solid #e4e7ed;
}

.description p {
  font-size: 14px;
  line-height: 1.6;
  color: #606266;
  margin: 0;
}

.requirements-grid {
  margin-top: 16px;
}

.requirement-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f2f5;
}

.requirement-item .label {
  font-weight: 500;
  color: #909399;
  min-width: 80px;
}

.requirement-item .value {
  color: #2c3e50;
  font-weight: 500;
}

.contact-info {
  margin-bottom: 24px;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid #f0f2f5;
}

.contact-item:last-child {
  border-bottom: none;
}

.contact-icon {
  color: #409eff;
  font-size: 18px;
}

.contact-details {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.contact-label {
  font-size: 12px;
  color: #909399;
}

.contact-value {
  font-size: 14px;
  color: #2c3e50;
  font-weight: 500;
}

.action-buttons {
  display: flex;
  flex-direction: column;
}

.action-btn {
  flex: 1;
  margin: 5px 0px;
}

.company-details {
  display: flex;
  gap: 16px;
  align-items: flex-start;
}

.company-info-detail h4 {
  margin: 0 0 8px 0;
  color: #2c3e50;
  font-size: 16px;
}

.company-desc {
  margin: 0;
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .detail-container {
    padding: 16px;
  }

  .header {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }

  .title {
    font-size: 24px;
  }

  .company-details {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }

  .requirements-grid .el-col {
    margin-bottom: 8px;
  }
}
</style>
